package model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import domein.Evenement;

public class EvenementenIO extends DbAbstract {

	public EvenementenIO() {
		super();
	}

	public Evenement getEvent(int id) {
		Evenement e = new Evenement(0, "", "", "", 0, 0, "", "");
		try {
			super.makeConnection();
			ResultSet rs = super.makeResultSet("SELECT * FROM event");
			while (rs.next()) {
				int idCheck = (rs.getInt("eventID"));
				if (idCheck == id) {
					e.setEventID(id);
					e.setNaam(rs.getString("naam"));
					e.setSubtitel(rs.getString("subtitel"));
					e.setDatum(rs.getString("datum"));
					e.setLocatieID(rs.getInt("locatieID"));
					e.setProgrammaID(rs.getInt("programmaID"));
					e.setContactPersoon(rs.getString("contactpersoon"));
					e.setOrganisator(rs.getString("organisator"));
				}
			}
			super.closeConnectRst();
			return e;
		} catch (Exception ex) {
			super.closeConnectRst();
			return e;
		}
	}
	
	// String query = "select * from event where naam like '%"+value+"%'OR contactpersoon like '%"+value+"%'"
	public ArrayList<Evenement> SearchListEvenementen(String value) {
		ArrayList<Evenement> eList = new ArrayList<Evenement>();
		try {
			super.makeConnection();
			ResultSet rs = super.makeResultSet("select * from event where naam like '%"+value+"%'OR contactpersoon like '%"+value+"%' ORDER BY datum ASC");
			while(rs.next()){
				int EventID = rs.getInt("eventID");
				String naam = rs.getString("naam");
				String subtitel = rs.getString("subtitel");
				String Datum  = rs.getString("datum");
				int locatieID = rs.getInt("locatieID");
				int programmaID = rs.getInt("programmaID");
				String contactPersoon = rs.getString("contactpersoon");
				String organisator = rs.getString("organisator");
				Evenement e = new Evenement(EventID, naam, subtitel, Datum, locatieID, programmaID, contactPersoon, organisator);
				eList.add(e);
			}
		} catch (SQLException ex) {
			System.out.println("Lijst evenementen ophalen gefaald.");
		}
		super.closeConnectRst();
		return eList; 
	}
	public ArrayList<Evenement> listEvenementen() {
		ArrayList<Evenement> eList = new ArrayList<Evenement>();
		try {
			super.makeConnection();
			ResultSet rs = super.makeResultSet("SELECT * FROM event ORDER BY datum ASC");
			while(rs.next()){
				int EventID = rs.getInt("eventID");
				String naam = rs.getString("naam");
				String subtitel = rs.getString("subtitel");
				String Datum  = rs.getString("datum");
				int locatieID = rs.getInt("locatieID");
				int programmaID = rs.getInt("programmaID");
				String contactPersoon = rs.getString("contactpersoon");
				String organisator = rs.getString("organisator");
				Evenement e = new Evenement(EventID, naam, subtitel, Datum, locatieID, programmaID, contactPersoon, organisator);
				eList.add(e);
			}
		} catch (SQLException ex) {
			System.out.println("Lijst evenementen ophalen gefaald.");
		}
		super.closeConnectRst();
		return eList; 
	}
	
	public void voegEvenementToe(Evenement e){
		String naam = e.getNaam();
		String subTitel = e.getSubtitel();
		String datum = e.getDatum();
		int locatieID = e.getLocatieID();
		int programmaID = 1;
		String contactPersoon = e.getContactPersoon();
		String organisator = e.getOrganisator();

		String query = "INSERT INTO event (naam, subtitel, datum, locatieID, contactpersoon,organisator)";
		query += "VALUES('" + naam + "', '" + subTitel + "',  '" + datum + "', '" + locatieID
				+ "', '" + contactPersoon + "', '"+ organisator + "')";

		try {
			super.addUpdateRecord(query);
			super.closeConnectRst();

		} catch (Exception ex) {

		}
	}
	
	public void pasEvenementAan(Evenement e){
		String naam = e.getNaam();
		String subTitel = e.getSubtitel();
		String datum = e.getDatum();
		int locatieID = e.getLocatieID();
		String contactPersoon = e.getContactPersoon();
		String organisator = e.getOrganisator();

		String query = "UPDATE event SET naam='"+naam+"', subtitel='"+subTitel+"', datum='"+datum+"', locatieid='"+locatieID+"', contactpersoon='"+contactPersoon+"', organisator='"+organisator+"'  where eventID='"+e.getEventID()+"'";

		try {
			super.addUpdateRecord(query);
			super.closeConnectRst();

		} catch (Exception ex) {

		}
	}
	
	public void linkProgramma(int eId, int pId){
		String query = "UPDATE event SET programmaID='"+pId+"' where eventID='"+eId+"'";

		try {
			super.addUpdateRecord(query);
			super.closeConnectRst();

		} catch (Exception ex) {

		}
	}
}
